package com.medical.acc.web.controller;

import com.medical.common.JsonResult;
import com.medical.acc.model.People;
import com.medical.acc.service.PeopleService;

import java.text.ParseException;
import java.util.*;

import com.medical.common.Constant;
import com.medical.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/people")
public class PeopleController {
    @Autowired
    private PeopleService peopleService;
    @Autowired
    private UserService userService;

    //返回people的总信息的总条数
    @ApiOperation(value="返回people的总信息的总条数", notes="返回people的总信息的总条数")
    @RequestMapping(value="", method=RequestMethod.GET)
    public JsonResult selectAllPeople() {
        List<People> list=peopleService.selectAllPeople();
        return new JsonResult("200","",list);
    }


    /**
     *
     * @param query
     * @param page
     * @param pageSize
     * @param sortColumn
     * @param sortDir
     * @return peopleList 的json数组
     */
    //返回携带分页的people的总信息json数组
    @ApiOperation(value="返回people的总数组", notes="返回people的总数组")
    @RequestMapping(value="/getpeople",method = RequestMethod.GET)
    public JsonResult selectAllPeopleAjax(@RequestParam(value = "query" ,required = false) String query, @RequestParam(value = "page" ,required = false) Integer page,
                                          @RequestParam(value = "pageSize" ,required = false) Integer pageSize, @RequestParam(value = "sortColumn" ,required = false) String sortColumn,
                                          @RequestParam(value = "sortDir" ,required = false) String sortDir){

        return new JsonResult(peopleService.searchList(query, page,pageSize , sortColumn, sortDir));
    }
    //健康专家栏获取看诊医生信息的api
    @ApiOperation(value="返回看诊医生集合", notes="看诊医生信息集合")
    @RequestMapping(value = "/doctor",method=RequestMethod.GET)
    public JsonResult selectAllDoctor(){

        return new JsonResult(userService.selectUsersByRoleName("看诊医生"));
    }

    //健康专家栏直接获取看诊医生的名字信息的api
    @RequestMapping(value = "/doctorname",method=RequestMethod.GET)
    public JsonResult selectDoctorName(){

        return new JsonResult(userService.selectDoctorName("看诊医生"));
    }

    //根据id获取对象
    @ApiOperation(value="根据id获取对象", notes="根据id获取对象")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getPeopleById (@PathVariable String id){
        try {
            return new JsonResult("200", "", peopleService.selectByPeopleId(id));
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }
    /**
     * @param fullname
     * @param doctorId
     * @param phone
     * @param birthday
     * @param gender
     * @param address
     * @param remark
     * @param cardId
     * @return
     */
    //新增客户信息
    @ApiOperation(value="新增", notes="新增")
    @RequestMapping(value = "/postpeople",method = RequestMethod.POST)
    public JsonResult savePeople(@RequestParam(value = "fullname",required = false) String fullname
                                ,@RequestParam(value = "doctorId",required = false) String doctorId,@RequestParam(value = "phone",required = false) String phone
                                ,@RequestParam(value = "birthday",required = false) String birthday,@RequestParam(value = "gender",required = false) Boolean gender
                                ,@RequestParam(value = "address",required = false) String address,@RequestParam(value = "remark",required = false) String remark
                                ,@RequestParam(value = "cardId",required = false) String cardId){
        People people=new People();
        people.setDoctorId(doctorId);
        people.setFullname(fullname);
        people.setPhone(phone);
        people.setAddress(address);
        people.setGender(gender);
        people.setCardId(cardId);
        people.setRemark(remark);
        Date brithday1 = new Date();
        try {
            brithday1 = DateUtils.parseDate(birthday, Constant.DATE_PATTERN);
        }catch (ParseException ex){
            ex.printStackTrace();
        }
        people.setBirthday(brithday1);
        try{
        peopleService.savePeople(people);
        return new JsonResult("200","",peopleService.selectByPeopleId(people.getId()));

        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
        }


    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patchpeople/{id}",method = RequestMethod.POST)
    public JsonResult patchPeople(@PathVariable String id,@RequestParam(value = "fullname",required = false) String fullname
                                 ,@RequestParam(value = "doctorId",required = false) String doctorId,@RequestParam(value = "phone",required = false) String phone
                                 ,@RequestParam(value = "birthday",required = false) String birthday,@RequestParam(value = "gender",required = false) Boolean gender
                                 ,@RequestParam(value = "address",required = false) String address,@RequestParam(value = "remark",required = false) String remark
                                 ,@RequestParam(value = "cardId",required = false) String cardId){
        People people=peopleService.selectByPeopleId(id);
        people.setDoctorId(doctorId);
        people.setFullname(fullname);
        people.setPhone(phone);
        people.setAddress(address);
        people.setGender(gender);
        people.setCardId(cardId);
        people.setRemark(remark);
        Date brithday1 = new Date();
        try {
            brithday1 = DateUtils.parseDate(birthday, Constant.DATE_PATTERN);
        }catch (ParseException ex){

        }
        people.setBirthday(brithday1);
        try{
        peopleService.updateByPrimaryKey(people);
        return new JsonResult("200","",peopleService.selectByPeopleId(id));
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
        }

    //删除
    @ApiOperation(value="删除", notes="删除")     @RequestMapping(value = "/deletepeople/{id}",method = RequestMethod.POST)
    public JsonResult deletePeople(@PathVariable String id){
        try {
            peopleService.deletePeople(id);

            return new JsonResult("200","删除成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
    }

    /**
     *
     * @param userId
     * @param peopleId
     * @return
     */
    //绑定用户(在acc_people中加入userid)
    @ApiOperation(value="绑定用户", notes="绑定用户(在acc_people中加入userid)")
    @RequestMapping(value = "/bind",method = RequestMethod.POST)
    public JsonResult bind(@RequestParam(value = "userId",required = false) String userId,
                           @RequestParam(value = "peopleId",required = false) String peopleId){
        try{
        peopleService.bindUser(peopleId, userId);
        return new JsonResult("200","");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
        }

    //解绑用户（在acc_people中把userid变为空值）
    @ApiOperation(value="解绑用户", notes="解绑用户（在acc_people中把userid变为空值）")
    @RequestMapping(value = "/unbind",method = RequestMethod.POST)
    public JsonResult unbind(@RequestParam(value = "peopleId",required = false) String peopleId){
        try{
        peopleService.unbindUser(peopleId);
            return new JsonResult("200","");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }


    //新增绑定用户（在acc_user_people中添加新数据）
    @ApiOperation(value="新增绑定用户", notes="新增绑定用户（在acc_user_people中添加新数据）")
    @RequestMapping(value = "/binduser",method = RequestMethod.POST)
    public JsonResult bindUser(@RequestParam(value = "userId",required = false) String userId,
                               @RequestParam(value = "peopleId",required = false) String peopleId){
        try{
        peopleService.bindUserPeople(peopleId, userId);
            return new JsonResult("200","");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }
    //删除绑定用户（在acc_user_people中添加新数据）
    @ApiOperation(value="删除绑定用户", notes="删除绑定用户（在acc_user_people中删除）")
    @RequestMapping(value = "/unbinduser",method = RequestMethod.POST)
    public JsonResult unbindUser(@RequestParam(value = "userId",required = false) String userId,
                                 @RequestParam(value = "peopleId",required = false) String peopleId){
        try{
        peopleService.unbindUserPeople(peopleId, userId);
            return new JsonResult("200","");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

}
